package com.ruoyi.api.user;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.api.user.requestVo.HealthDayDataRequestVo;
import com.ruoyi.api.user.requestVo.HealthRateSendListRequestVo;
import com.ruoyi.api.user.requestVo.HealthRateSendRequestVo;
import com.ruoyi.api.user.requestVo.HealthSendRequestVo;
import com.ruoyi.api.user.response.HealthDataResponseVo;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.user.domain.Health;
import com.ruoyi.user.domain.HealthRate;
import com.ruoyi.user.domain.SleepDetail;
import com.ruoyi.user.domain.bo.HealthResponseBo;
import com.ruoyi.user.service.IHealthRateService;
import com.ruoyi.user.service.IHealthService;

import io.swagger.annotations.ApiOperation;

/**
 * 问题反馈 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-23
 */
@Controller
@RequestMapping("/api/health")
public class ApiHealthController extends BaseController
{
    private String prefix = "api:health";
	
    @Autowired
	private IHealthService healthService;
    @Autowired
    private IHealthRateService healthRateService;
	
	/**
	 * 血压图表
	 */
	@PostMapping("/bpFlow")
	@ResponseBody
	@ApiOperation(value="血压图表", notes="血压图表",produces = "application/json")
	public HashMap<String, Object> bpFlow(@RequestBody HealthDayDataRequestVo dayDataRequestVo)
	{		
		HealthDataResponseVo vo = new HealthDataResponseVo();
		LinkedList<HealthResponseBo> boList = healthService.selectHealthListByday(dayDataRequestVo.getmCode(), dayDataRequestVo.getDataTime());
		vo.setList(boList);
		int high = 0;
		int low = 0;
		int count =0;
		for(HealthResponseBo bo:boList){
			if(count==0){
				high = bo.getSdp().intValue();
				low = bo.getDbp().intValue();
				vo.setHighBp(bo.getSdp()+"/"+bo.getDbp());
				vo.setLowBp(bo.getSdp()+"/"+bo.getDbp());
			}else{
				if(high<bo.getSdp().intValue()){
					high = bo.getSdp().intValue();
					vo.setHighBp(bo.getSdp()+"/"+bo.getDbp());
				}
				if(low>bo.getDbp().intValue()){
					low = bo.getDbp().intValue();
					vo.setLowBp(bo.getSdp()+"/"+bo.getDbp());
				}
			}
			count++;
		}
		return successApp(vo);
	}
	
	/**
	 * 心率图表
	 */
	@PostMapping("/heartRateFlow")
	@ResponseBody
	@ApiOperation(value="心率图表", notes="心率图表",produces = "application/json")
	public HashMap<String, Object> heartRateFlow(@RequestBody HealthDayDataRequestVo dayDataRequestVo)
	{		
		HealthDataResponseVo vo = new HealthDataResponseVo();
		LinkedList<HealthResponseBo> boList = healthRateService.selectHealthListByday(dayDataRequestVo.getmCode(), dayDataRequestVo.getDataTime());
		vo.setList(boList);
		int high = 0;
		int low = 0;
		int count =0;
		for(HealthResponseBo bo:boList){
			if(count==0){
				high = bo.getHeartRate().intValue();
				low = bo.getHeartRate().intValue();
				vo.setHighHeartRate(bo.getHeartRate().toString());
				vo.setLowHeartRate(bo.getHeartRate().toString());
			}else{
				if(high<bo.getHeartRate().intValue()){
					high = bo.getHeartRate().intValue();
					vo.setHighHeartRate(bo.getHeartRate().toString());
				}
				if(low>bo.getHeartRate().intValue()){
					low = bo.getHeartRate().intValue();
					vo.setLowHeartRate(bo.getHeartRate().toString());
				}
			}
			count++;
		}
		return successApp(vo);
	}
	
	/**
	 * 血压数据上传
	 */
	@PostMapping("/sendHealth")
	@ResponseBody
	@ApiOperation(value="血压数据上传", notes="血压数据上传",produces = "application/json")
	public HashMap<String, Object> sendHealth(@RequestBody HealthSendRequestVo requestVo)
	{		
		if(requestVo!=null&&requestVo.getmCode()!=null){
			try {
				Health health = new Health();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				health.setCreateTime(sdf.parse(requestVo.getDateTime()));
				health.setDbp(requestVo.getDbp());
				health.setSdp(requestVo.getSdp());
				health.setHeartRate(requestVo.getHeartRate());
				health.setImei(requestVo.getmCode());
				healthService.insertHealth(health);
				HealthRate healthRate = new HealthRate();
				healthRate.setCreateTime(new Date());
				healthRate.setHeartRate(health.getHeartRate());
				healthRate.setImei(health.getImei());
				/*health.setUpdateBy(requestVo.getDateTime());*/
				healthRateService.insertHealth(healthRate);
			} catch (Exception e) {
				e.printStackTrace();
				return errorApp("hl-0003","血压数据上传异常");
			}
			return successApp("");
		}else{
			return errorApp("hl-0003","血压数据上传异常");
		}
	}
	
	/**
	 * 心率数据上传
	 */
	@PostMapping("/sendHealthRate")
	@ResponseBody
	@ApiOperation(value="心率数据上传", notes="心率数据上传",produces = "application/json")
	public HashMap<String, Object> sendHealthRate(@RequestBody HealthRateSendRequestVo requestVo)
	{		
		if(requestVo!=null&&requestVo.getmCode()!=null){
			try {
				//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				HealthRate slpdParam = new HealthRate();
				slpdParam.setImei(requestVo.getmCode());
				slpdParam.setUpdateBy(requestVo.getDateTime());
				List<HealthRate> listSpld = healthRateService.selectHealthList(slpdParam);
				String ids = "";
				int count = 0 ;
				for(HealthRate sld:listSpld){
					if(count==0){
						ids += sld.getId();
					}else{
						ids += ","+sld.getId();
					}
					count++;
				}
				healthRateService.deleteHealthByIds(ids);
				List<HealthRateSendListRequestVo> list = requestVo.getList();
				for(HealthRateSendListRequestVo vo:list){
					HealthRate health = new HealthRate();
					health.setCreateTime(sdf1.parse(vo.getDateTime()));
					health.setHeartRate(vo.getHeartRate());
					health.setImei(requestVo.getmCode());
					health.setUpdateBy(requestVo.getDateTime());
					healthRateService.insertHealth(health);
				}
			} catch (Exception e) {
				e.printStackTrace();
				return errorApp("hl-0004","心率数据上传异常");
			}
			return successApp("");
		}else{
			return errorApp("hl-0004","心率数据上传异常");
		}
	}

}
